package com.lankystar.sos.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lankystar.sos.model.Room;
import com.lankystar.sos.model.SosOrder;
import com.lankystar.sos.model.User;
import com.lankystar.sos.service.IAddDataInterface;
import com.lankystar.sos.service.IRoomInterface;
import com.lankystar.sos.service.ISosOrderInterface;
import com.lankystar.sos.service.IUserInterface;
import com.lankystar.util.ResUtil;
import com.opensymphony.xwork2.ActionSupport;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

@ParentPackage("sos")
@Namespace("/")
@Component
@Scope("prototype")
@Results({
	@Result(name="success",type="json")
})
public class SosOrderAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	@Autowired
	private ISosOrderInterface sosOrderInterface;
	@Autowired
	private IRoomInterface roomInterface;
	@Autowired
	private IUserInterface userInterface;
	private String userPhone;
	private double longitude;
	private double latitude;
	private Integer sosId;
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public Integer getSosId() {
		return sosId;
	}
	public void setSosId(Integer sosId) {
		this.sosId = sosId;
	}
	
	/**
	 * 有响应单信息返回  ：响应单、求救单信息；
	 * 没有响应单信息返回：返回求救单信息；
	 * return
	 */
	@Action(value="queryUserAndRespOrder")
	public String queryUserAndRespOrderMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			Map<String, Object> oblist = sosOrderInterface.queryUserAndRespMethod(sosId);
			map.put("datas", oblist);
			map.put("code", 1);
			map.put("msg","求救单相关信息查询成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg","求救单相关信息查询失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	
	/**
	 * 生成求救单   并推送
	 * @return
	 */
	@Action(value="addSosOrder")
	public String addSosOrderMethod() {
		Map<String, Object> map = new HashMap<>();
		List<SosOrder> sosOrders = sosOrderInterface.querySosOrderByuserPhoneAndsosOrderStatusMethod(userPhone);
		try {
			//if(!sosOrders.isEmpty()){
				SosOrder sosOrder =sosOrderInterface.addSosOrderMethod(userPhone, longitude, latitude);
				/**
				 * 查找附近所有施救者      用于推送消息
				 */
				List<User> userlist = userInterface.queryRespUserlistMethod(1,longitude, latitude);
				String[] arr = new String[userlist.size()];
				int i=0;
				for (User user : userlist) {
					arr[i] = String.valueOf(user.getUserPhone());
					i++;
				}
					//产生聊天室
					Room room = new Room();
					room.setSos_id(sosOrder.getSosId());
					roomInterface.addRoomMethod(room);
					/**
					 * 调用推送方法，附近没有人不推送
					 */
				if(arr.length!=0){
					PushExample.sendPush(arr, sosOrder.getSosId(), 1);
				}
					Map<String, Object> sosmap = new HashMap<>();
					sosmap.put("sosId", sosOrder.getSosId());
					map.put("data", sosmap);
					map.put("code", 1);
					map.put("msg", "求救单生成成功！！");
			/*}else{
				map.put("code", 0);
				map.put("msg", "求救单生成失败,存在未完成的求救单！！");
			}*/
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "求救单生成失败！！");
		}
	    ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 通过sosId查找求救单信息  、求救单状态、求救者信息;
	 * 			响应单信息  、 施救者信息
	 * @return
	 */
	@Action(value="querySosOrderBySosId")
	public String querySosOrderBySosIdMethos(){
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", sosOrderInterface.queryUserAndRespMethod(sosId));
			map.put("code", 1);
			map.put("msg", "求救单信息查询成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "求救单信息查询失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
}
